package api

import (
	"api/config"
	"api/internal/api/request"
	"api/internal/api/response"
	"github.com/gin-gonic/gin"
	seckill "seckill/proto"
)

func Create(c *gin.Context) {
	var json *request.Create
	if err := c.ShouldBindJSON(&json); err != nil {
		response.Response(c, 400, err.Error(), nil)
		return
	}
	kill, err := config.SecKillSrv.CreateSecKill(c, &seckill.CreateSecKillRequest{
		GoodsName:    json.GoodsName,
		SecKillPrice: float32(json.SecKillPrice),
		GoodsPrice:   float32(json.GoodsPrice),
		Stock:        int64(json.Stock),
		SecKillType:  json.SecKillType,
		StartTime:    int64(json.StartTime),
		EndTime:      int64(json.EndTime),
	})
	if err != nil {
		response.Response(c, 400, err.Error(), nil)
		return
	}
	response.Response(c, 200, "添加成功", kill)
}

func Update(c *gin.Context) {
	var json *request.Update
	if err := c.ShouldBindJSON(&json); err != nil {
		response.Response(c, 400, err.Error(), nil)
		return
	}
	kill, err := config.SecKillSrv.UpdateSecKill(c, &seckill.UpdateSecKillRequest{
		GoodsName:    json.GoodsName,
		SecKillPrice: float32(json.SecKillPrice),
		GoodsPrice:   float32(json.GoodsPrice),
		Stock:        int64(json.Stock),
		SecKillType:  json.SecKillType,
		StartTime:    int64(json.StartTime),
		EndTime:      int64(json.EndTime),
		Id:           int64(json.Id),
	})
	if err != nil {
		response.Response(c, 400, err.Error(), nil)
		return
	}
	response.Response(c, 200, "修改成功", kill)
}

func Delete(c *gin.Context) {
	var json *request.Delete
	if err := c.ShouldBindJSON(&json); err != nil {
		response.Response(c, 400, err.Error(), nil)
		return
	}
	kill, err := config.SecKillSrv.DeleteSecKill(c, &seckill.DeleteSecKillRequest{
		Id: int64(json.Id),
	})
	if err != nil {
		response.Response(c, 400, err.Error(), nil)
		return
	}
	response.Response(c, 200, "删除成功", kill)
}

func List(c *gin.Context) {
	var json *request.List
	if err := c.ShouldBindJSON(&json); err != nil {
		response.Response(c, 400, err.Error(), nil)
		return
	}
	kill, err := config.SecKillSrv.SecKillList(c, &seckill.SecKillListRequest{
		Id: int64(json.Id),
	})
	if err != nil {
		response.Response(c, 400, err.Error(), nil)
		return
	}
	response.Response(c, 200, "展示成功", kill)
}

func Type(c *gin.Context) {
	var json *request.Types
	if err := c.ShouldBindJSON(&json); err != nil {
		response.Response(c, 400, err.Error(), nil)
		return
	}
	kill, err := config.SecKillSrv.TypeSecKill(c, &seckill.TypeSecKillRequest{Type: json.TypesSec})
	if err != nil {
		response.Response(c, 400, err.Error(), nil)
		return
	}
	response.Response(c, 200, "计算成功", kill)
}

func Price(c *gin.Context) {
	var json *request.Price
	if err := c.ShouldBindJSON(&json); err != nil {
		response.Response(c, 400, err.Error(), nil)
		return
	}
	kill, err := config.SecKillSrv.PriceSecKill(c, &seckill.PriceSecKillRequest{
		StartPrice: float32(json.StartPrice),
		EndPrice:   float32(json.EndPrice),
	})
	if err != nil {
		response.Response(c, 400, err.Error(), nil)
		return
	}
	response.Response(c, 200, "计算成功", kill)
}
